
package malateproject;

import java.sql.ResultSet;
import javax.swing.DefaultListModel;

public class FrontPage extends javax.swing.JFrame {

    /** Creates new form FrontPage */
    public FrontPage() {
        initComponents();
        setLocationRelativeTo(null);
        this.setSize(400,500);
        this.setBounds(200, 70, 800, 620);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextFieldIDnum = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButtonTimeIn = new javax.swing.JButton();
        jButtonTimeOut = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListTimedIn = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 0, 0));
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 3, 30)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("    Malate Project");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(440, 120, 250, 40);

        jTextField1.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        getContentPane().add(jTextField1);
        jTextField1.setBounds(470, 180, 180, 19);
        getContentPane().add(jTextFieldIDnum);
        jTextFieldIDnum.setBounds(100, 150, 130, 20);

        jPasswordField1.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });
        getContentPane().add(jPasswordField1);
        jPasswordField1.setBounds(470, 220, 180, 19);

        jButton1.setText("Login");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(500, 270, 100, 30);

        jButton2.setText("Register");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(660, 360, 80, 30);

        jButton3.setText("About Us");
        getContentPane().add(jButton3);
        jButton3.setBounds(660, 413, 80, 30);

        jButtonTimeIn.setText("Time In");
        jButtonTimeIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTimeInActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonTimeIn);
        jButtonTimeIn.setBounds(50, 200, 80, 40);

        jButtonTimeOut.setText("Time Out");
        jButtonTimeOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTimeOutActionPerformed(evt);
            }
        });
        getContentPane().add(jButtonTimeOut);
        jButtonTimeOut.setBounds(150, 200, 80, 40);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Username");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(410, 180, 60, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Password");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(410, 220, 60, 15);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Checked-in Users");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(90, 240, 130, 40);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("ID Number");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(30, 150, 60, 20);

        jListTimedIn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListTimedInMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListTimedIn);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(90, 280, 120, 140);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/MalateBG.E-res.jpg"))); // NOI18N
        getContentPane().add(jLabel2);
        jLabel2.setBounds(0, 0, 760, 600);
        jLabel2.getAccessibleContext().setAccessibleName("jLabelBG");

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    
    /*
        DBManager.DatabaseManager dm = new DBManager.DatabaseManager(); Function later
    
        String username = this.jTextField1.getText();
        char[] password = this.jPasswordField1.getPassword();
     
        if (dm.login(username, password)){
            Main.setActive(dm.getUser(username));
            
            if (dm.isValid(username)) {
                new MainPage().setVisible(true);
       
     *          }
            
    
    */
    
   
}//GEN-LAST:event_jButton1ActionPerformed

private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jPasswordField1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    
    
    this.setVisible(false);
    new Register().setVisible(true);
    
}//GEN-LAST:event_jButton2ActionPerformed
    /* Updates a listModel that contains a list of the members currently timed in.
     * 
     * Return type: void
     */
    private void refreshListTimedIn(ResultSet rs) {
        DefaultListModel listModel = new DefaultListModel();
        
        try {
            while (rs.next()) {
                listModel.addElement(rs.getString("member_IDnumber"));
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        
        jListTimedIn.setModel(listModel);
    }
    
    /* Sets selected item's value as jTextFieldIDnum's value.
     * 
     * Return type: void
     */
    private void jListTimedInMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListTimedInMouseClicked
        if (jListTimedIn.getSelectedValue() != null) {
            jTextFieldIDnum.setText(jListTimedIn.getSelectedValue().toString());
        }
    }//GEN-LAST:event_jListTimedInMouseClicked

    /* Times in the member with the ID number from jTextFieldIDnum
     * 
     * Return type: void
     */
    private void jButtonTimeInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTimeInActionPerformed
        Objects.Member mem = new Objects.Member();
        DBM.DBManager dbm = new DBM.DBManager();
        
        mem.setIdNo(jTextFieldIDnum.getText());
        if (mem.isLoggedInResidency()) {
            // warning: "You are already logged in"
        }
        else {
            if (mem.timeIn()) {
                // display: "You have successfully timed in"
                refreshListTimedIn(dbm.getLoggedInMembersList());
                jTextFieldIDnum.setText("");
            }
            else {
                /*error message: "Error. You could not be logged in to the 
                 *residency system. Please contact the Documentations Manager."*/
            }
        }
    }//GEN-LAST:event_jButtonTimeInActionPerformed

    /* Times out the member with the ID number from jTextFieldIDnum
     * 
     * Return type: void
     */
    private void jButtonTimeOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTimeOutActionPerformed
        Objects.Member mem = new Objects.Member();
        DBM.DBManager dbm = new DBM.DBManager();
        
        mem.setIdNo(jTextFieldIDnum.getText());
        if (mem.isLoggedInResidency()) {
            if (mem.timeOut()) {
                // display: "You have successfully timed out"
                refreshListTimedIn(dbm.getLoggedInMembersList());
                jTextFieldIDnum.setText("");
            }
        }
        else {
            // display: "You have not yet timed in."
        }
    }//GEN-LAST:event_jButtonTimeOutActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrontPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrontPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrontPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrontPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrontPage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButtonTimeIn;
    private javax.swing.JButton jButtonTimeOut;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jListTimedIn;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextFieldIDnum;
    // End of variables declaration//GEN-END:variables
}
